layui.use(['laydate', 'laypage', 'form', 'layer'], function () {
    let laypage = layui.laypage;
    let form = layui.form;
    let layer = layui.layer;
    //监听提交
    form.on('submit(esSearch)',
        function (data) {
            //分页完整功能
            laypage.render({
                elem: 'demo7',
                count: esSearchCount(data.field.feedback),
                layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip'],
                jump: function (obj) {
                    $.ajax({
                        url: "http://localhost:8080/back/message/esSearch",
                        dataType: "json",
                        // data: {"page": obj.curr, "limit": obj.limit},
                        data: {"pattern": data.field.feedback},
                        headers:{'token':getCookie("keji_token")},
                        success: function (res) {
                            console.log("后端返回数据：",res.data.data[1])
                            //模糊查询没有返回值，刷新当前页面
                            if (res.data.length == 0) {
                                layer.msg("不存在该关键字", function () {
                                    location.reload();
                                })
                            }

                            var html;
                            for (var i = 0; i < res.data.data.length; i++) {
                                html += ' <tr>\n' +
                                    '                                    <td>\n' +
                                    '                                      <input type="checkbox" name="id" value="1"   lay-skin="primary"> \n' +
                                    '                                    </td>\n' +
                                    '                                    <td>' + res.data.data[i].id + '</td>\n' +
                                    '                                    <td>' + res.data.data[i].subject + '</td>\n' +
                                    '                                    <td>' + res.data.data[i].email + '</td>\n' +
                                    '                                    <td>' + res.data.data[i].feedback + '</td>\n' +
                                    '                                    <td class="td-status">\n' +
                                    '                                      <span class="layui-btn layui-btn-normal layui-btn-mini ' + (res.data.data[i].enable == 1 ? "" : "layui-btn-disabled") + ' ">' + (res.data.data[i].enable == 1 ? "已回复" : "未回复") + '</span></td>\n' +
                                    '                                    <td class="td-manage">\n' +
                                    '                                      <a title="回复"  onclick="sendEsInfo(' + res.data.data[i].id + ')" href="javascript:;">\n' +
                                    '                                        <i class="layui-icon">&#xe642;</i>\n' +
                                    '                                      </a>\n' +
                                    '                                      <a title="删除" onclick="EsMemberdel(this,' + res.data.data[i].id + ')" href="javascript:;">\n' +
                                    '                                        <i class="layui-icon">&#xe640;</i>\n' +
                                    '                                      </a>\n' +
                                    '                                    </td>\n' +
                                    '                                  </tr>'

                            }
                            $("#userInfo").html(html);

                        },
                        error: function (jqXHR, textStatus, errorThrown) {
                            /*用户未登录*/
                            if (jqXHR.status == 401) {
                                window.location.href = "http://localhost:63342/spring-boot-front-page/html/login.html"
                            }

                        }
                    })
                }
            })
            return false
        }
    )
})


//得到记录条数
function esSearchCount(data) {
    let count = 0;
    $.ajax({
        url: "http://localhost:8080/back/message/esSearchCount",
        dataType: "json",
        data: {"pattern": data},
        /*设置请求头*/
        headers:{'token':getCookie("keji_token")},
        async: false,//关闭异步
        success: function (res) {
            count = res.data;

        },
        error: function (jqXHR, textStatus, errorThrown) {
            /*用户未登录*/
            if (jqXHR.status == 401) {
                window.location.href = "http://localhost:63342/spring-boot-front-page/html/login.html"
            }

        }
    })
    return count;
}


//编辑普通信息

function sendEsInfo(id) {
    layui.use(['layer'], function () {
        let layer = layui.layer;
        sessionStorage.setItem("ReplayId", id);

        xadmin.open('回复', '../../html/message/replay.html', 600, 400);

    })
}

//删除某一行用户
function EsMemberdel(obj, id) {
    layui.use(['layer'], function () {
        let message = {"id": id}
        layer.confirm('确认要删除吗？', function () {
            $.ajax({
                url: "http://localhost:8080/back/message/delete",
                type: "post",
                /*设置请求头*/
                headers: {"Authorization": "Bearer " + localStorage.getItem("jwtToken")},
                dataType: "json",
                data: JSON.stringify(message),
                contentType: "application/json",
                success: function (res) {
                    if (res.msg == "success") {
                        $(obj).parents("tr").remove();
                        layer.msg('已删除!', {icon: 1, time: 1000});
                    } else {
                        layer.msg('删除失败!', {icon: 1, time: 1000});
                    }


                },


            })
        });
    })
}function setCookie(name,value)
{
    var Days = 30;
    var exp = new Date();
    exp.setTime(exp.getTime() + Days*24*60*60*1000);
    document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
}
function clearCookie(name) {
    setCookie(name, "", -1);
}
function clearAllCookie() {
    var date=new Date();
    date.setTime(date.getTime()-10000);
    var keys=document.cookie.match(/[^ =;]+(?=\=)/g);
    console.log("需要删除的cookie名字："+keys);
    if (keys) {
        for (var i =  keys.length; i--;)
            document.cookie=keys[i]+"=0; expire="+date.toGMTString()+"; path=/";
    }
}
function getCookie(name)
{
    var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
    if(arr=document.cookie.match(reg))
        return unescape(arr[2]);
    else
        return null;
}
